/*
 * Copyright (c) 2000-2017 wanglei. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */

package com.wlit.ui;


import com.wlit.JavaBrowserApplication;
import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.browser.CefMessageRouter;
import org.cef.callback.CefQueryCallback;
import org.cef.handler.CefLifeSpanHandlerAdapter;
import org.cef.handler.CefMessageRouterHandler;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * @author wanglei.
 */
public final class TabFactory {




    public static Tab createTab(CefApp cefApp) {
        return createTab("about:blank",cefApp);
    }

    public static Tab createTab(String url,CefApp cefApp) {
        CefClient client = cefApp.createClient();

        //尝试处理打开新窗口问题
        client.addLifeSpanHandler(new LifeSpanHandler(cefApp));
        // (3) Create a simple message router to receive messages from CEF.
        CefMessageRouter.CefMessageRouterConfig config = new CefMessageRouter.CefMessageRouterConfig("get","set");
        CefMessageRouter msgRouter = CefMessageRouter.create(config);
        msgRouter.addHandler(new CefMessageRouterHandler() {
            @Override
            public boolean onQuery(CefBrowser cefBrowser, CefFrame cefFrame, long l, String s, boolean b, CefQueryCallback callback) {
                System.out.println(">>>"+s);
                callback.success("OK");
                return false;
            }

            @Override
            public void onQueryCanceled(CefBrowser cefBrowser, CefFrame cefFrame, long l) {

            }

            @Override
            public void setNativeRef(String s, long l) {
               System.out.println(">>>"+s+"@"+l);
            }

            @Override
            public long getNativeRef(String s) {
                System.out.println(">>>"+s);
                return 0;
            }
        },true);

        client.addMessageRouter(msgRouter);

        TabContent tabContent = new TabContent(cefApp,url);
        final TabCaption tabCaption = new TabCaption();
        tabCaption.setTitle("about:blank");
        tabContent.addPropertyChangeListener("PageTitleChanged", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                tabCaption.setTitle((String) evt.getNewValue());
            }
        });
        tabContent.getToolBar().setUrl(url);
        return new Tab(tabCaption, tabContent);
    }
}

/**
 * 打开新窗口处理
 */
class LifeSpanHandler extends CefLifeSpanHandlerAdapter {

    private CefApp cefApp;

    LifeSpanHandler(CefApp cefApp){
        this.cefApp = cefApp;
    }


    @Override
    public boolean onBeforePopup(CefBrowser browser, CefFrame frame, String target_url, String target_frame_name) {
        //返回true表示取消弹出窗口
        JavaBrowserApplication.insertTab(TabFactory.createTab(target_url, cefApp));
        return true;
    }
}